package com.joinway.framework.admin.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.joinway.framework.admin.core.service.IAdminWebService;
import com.joinway.framework.admin.core.service.NoticeService;
import com.joinway.framework.admin.core.service.file.FileConstants;
import com.joinway.framework.bean.exception.SessionExpiredException;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.security.annotation.Security;
import com.joinway.framework.bean.security.type.CheckAction;
import com.joinway.framework.core.controller.WebExceptionController;
import com.joinway.framework.core.plugin.PluginManager;
import com.joinway.framework.extension.utils.DataUtils;

@Controller
@RequestMapping("")
@Validated
public class AdminWebController extends WebExceptionController {

	private final static Logger log = LoggerFactory.getLogger(AdminWebController.class);
	
	@Autowired protected List<IAdminWebService> webServices;
	
	@Autowired protected NoticeService noticeService;
	
	@RequestMapping(value={"", "index"})
	@InputLog
	public ModelAndView index() throws Exception {
		return getService().index();
	}

	@RequestMapping(value="ssologin")
	@Security(action=CheckAction.Login)
	@InputLog
	public ModelAndView login(@RequestParam("token") String token, @LogIgnore HttpServletRequest request) throws Exception {
		return getService().ssoLogin(token, request);
	}

	@RequestMapping(value="logout")
//	@Logout
	public ModelAndView logout(@LogIgnore HttpServletRequest request) throws Exception {
		ModelAndView mv = getService().logout();
		
		try{
			request.getSession().invalidate();
		}catch(Exception e){
			log.warn("sessioin has already invalidate");
		}
		
		return mv;
	}
	
	@RequestMapping(value="favicon")
	@InputLog
	public ModelAndView favicon() throws Exception {
		return getService().favicon();
	}

	@RequestMapping(value="{page}")
	@Security
	@InputLog
	public ModelAndView main(@PathVariable("page") String page) throws Exception {
		ModelAndView mv = new ModelAndView(page);
		
		return getService().process(mv, page);
	}

	@RequestMapping(value="tabs/{page}")
	@Security
	@InputLog
	public ModelAndView tab(@PathVariable("page") String page, @LogIgnore HttpServletRequest request) throws Exception {
		return getService().tab(page, request);
	}
	
	@RequestMapping("push_log/detail")
	@Security
	@InputLog
	public ModelAndView page(@RequestParam("noticeId") String noticeId, @RequestParam("pageId") String pageId) throws SessionExpiredException {
		ModelAndView mv = new ModelAndView("page/push_log_detail");
		mv.addObject("noticeId", noticeId);
		mv.addObject("pageId", pageId);
		
		return mv;
	}
	
    @RequestMapping(value=FileConstants.NoticeFile.Path + "/{page}")
    @InputLog
    public ModelAndView acknowledgeNotice(@PathVariable("page") @NotBlank String page, @RequestParam(value="userId",required=false,defaultValue="") String userId) throws Exception {
    	noticeService.acknowledge(page, DataUtils.convertToInt(userId));
    	
        ModelAndView mv = new ModelAndView(FileConstants.NoticeFile.Path + "/" + page);
        
        return mv;
    }

    protected IAdminWebService getService(){
    	return PluginManager.getInstance(webServices);
    }
}

